package com.world.business.realm.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.world.business.realm.domain.Realm;
import com.world.business.realm.domain.query.RealmAddRequest;
import com.world.business.realm.domain.query.RealmUpdateRequest;
import com.world.business.realm.domain.vo.RealmVO;
import com.world.business.realm.mapper.RealmMapper;
import com.world.business.role.domain.Role;
import com.world.business.role.mapper.RoleMapper;
import com.world.common.constants.RedisKeyConstants;
import com.world.common.enumeration.DelFlagEnum;
import com.world.common.exception.BusinessException;
import com.world.common.security.SecurityUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 境界表 服务实现类
 * </p>
 *
 * @author 邓驴
 * @since 2023-05-16
 */
@Service
public class RealmServiceImpl extends ServiceImpl<RealmMapper, Realm> implements IRealmService {

    @Resource
    private RedisTemplate<String, String> redisTemplate;
    @Resource
    private RoleMapper roleMapper;

    @Override
    public Long add(RealmAddRequest request) {
        Realm realm = BeanUtil.copyProperties(request, Realm.class);
        this.baseMapper.insert(realm);
        return realm.getId();
    }

    @Override
    public RealmVO get(Long id) {
        Object o = redisTemplate.boundValueOps(RedisKeyConstants.REALM_KEY + ":" + id).get();
        if (BeanUtil.isEmpty(o)) {
            Realm realm = this.baseMapper.selectById(id);
            if (BeanUtil.isEmpty(realm)) {
                throw new BusinessException("数据不存在");
            }
            RealmVO vo = BeanUtil.copyProperties(realm, RealmVO.class);
            Role role = roleMapper.selectById(vo.getRoleId());
            if (BeanUtil.isNotEmpty(role)) {
                vo.setRoleName(role.getRoleName());
            }
            redisTemplate.boundValueOps(RedisKeyConstants.REALM_KEY + ":" + id).set(JSONObject.toJSONString(vo));
            return vo;
        }
        return JSONObject.parseObject(o.toString(), RealmVO.class);
    }

    @Override
    public void delete(Long id) {
        Realm realm = this.baseMapper.selectById(id);
        if (BeanUtil.isEmpty(realm)) {
            return;
        }
        realm.setDelFlag(DelFlagEnum.DELETE.getCode());
        this.baseMapper.updateById(realm);
        redisTemplate.delete(RedisKeyConstants.REALM_KEY + ":" + id);
    }

    @Override
    public void update(RealmUpdateRequest request) {
        Realm realm = this.baseMapper.selectById(request.getId());
        if (BeanUtil.isEmpty(realm)) {
            throw new BusinessException("数据不存在！");
        }
        LambdaUpdateWrapper<Realm> updateWrapper = Wrappers.lambdaUpdate(Realm.class);
        updateWrapper.eq(Realm::getId, realm.getId());
        updateWrapper.set(request.getRoleId() != null, Realm::getRoleId, request.getRoleId());
        updateWrapper.set(StrUtil.isNotBlank(realm.getName()), Realm::getName, request.getName());
        updateWrapper.set(StrUtil.isNotBlank(realm.getPid()), Realm::getPid, request.getPid());
        updateWrapper.set(StrUtil.isNotBlank(realm.getStatus()), Realm::getStatus, request.getStatus());
        updateWrapper.set(StrUtil.isNotBlank(realm.getRemark()), Realm::getRemark, request.getRemark());
        updateWrapper.set(StrUtil.isNotBlank(realm.getType()), Realm::getType, request.getType());
        updateWrapper.set(Realm::getRank, request.getRank());
        updateWrapper.set(Realm::getUpdateBy, SecurityUtils.getLoginUser().getName());
        updateWrapper.set(Realm::getUpdateById, SecurityUtils.getLoginUser().getId());
        updateWrapper.set(Realm::getUpdateTime, LocalDateTime.now());
        this.baseMapper.update(null, updateWrapper);
    }
}
